<route lang="yaml">
  meta:
    enabled: false
  </route>

<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'

defineOptions({ name: 'ShareCustomerForm' })

interface SysUserVO {
  id: number
  name: string
  phone: string
  wechat: string
  password: string
  address: string
}

const userFormRef = ref<FormInstance>()
const userForm = reactive<SysUserVO>({
  id: null,
  name: '',
  phone: '',
  wechat: '',
  password: '',
})

function validatePass(rule: any, value: any, callback: any) {
  if (ruleForm.id == null && value === '') {
    callback(new Error('请输入登录密码'))
  }
  else {
    if (userForm.password !== '') {
      if (!userFormRef.value) { return }
      userFormRef.value.validateField('password')
    }
    callback()
  }
}
const regex = /^1[3-9]\d{9}$/

function validatePhone(rule: any, value: any, callback: any) {
  if (value === '') {
    callback(new Error('请输入手机号'))
  }
  else if (regex.test(userForm.phone)) {
    callback(new Error('手机号格式不正确'))
  }
  else {
    callback()
  }
}

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 15, message: '字符数在2~15个字符', trigger: 'blur' },
  ],
  phone: [
    { validator: validatePhone, trigger: 'blur' },
  ],
  password: [
    { validator: validatePass, trigger: 'blur' },
  ],
})
async function submitForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    }
    else {
      console.log('error submit!', fields)
    }
  })
}

function resetForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  formEl.resetFields()
}

// 分配客户表单相关

interface CustomerVO {
  id: string
  name: string
  phone: string
  address: string
  level: number
  createTime: string
  wechat: string
  checked: boolean
}

const CustomerTableData = ref<CustomerVO[]>([
  { id: 10001, name: '李四', phone: '156****7754', wechat: '156****7754', address: '广东省深圳市宝安区西乡街道航城路2206号', level: 1, createTime: '2025-04-06 12:11:56' },
  { id: 10002, name: '李四', phone: '156****7754', wechat: '156****7754', address: '广东省深圳市宝安区西乡街道航城路2206号', level: 2, createTime: '2025-04-06 12:11:56' },
])

const shareCustomerFormModal = ref(false)
const shareCustomerFormTitle = ref('分配客户')

function closeShareCustomerForm() {
  shareCustomerFormModal.value = false
}

const searchCustomerFormRef = ref<FormInstance>()
const searchCustomerForm = ref({
  name: '',
})

const currentOperate = ref<string>('add')

const searchCustomer = ref({
  sysUserId: null,
  customerIds: [],
})

function submitShareFormHandle() {

}

const checkedTotal = ref(0)

function checkHandle(value) {
  checkedTotal.value = 0
  CustomerTableData.value.forEach((item) => {
    if (item.checked) {
      checkedTotal.value++
    }
  })
}

async function openShareCustomerForm(row: any, type: string) {
  console.log(row)
  shareCustomerFormModal.value = true
  currentOperate.value = type
  if (type == 'add') {
    shareCustomerFormTitle.value = '分配客户资源'
  }
  else {
    shareCustomerFormTitle.value = '调整客户资源'
  }
}
defineExpose({ openShareCustomerForm }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <el-dialog
    v-model="shareCustomerFormModal" :title="shareCustomerFormTitle"
    :align-center="true" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="closeShareCustomerForm()"
  >
    <div class="flex-start-center flex-wrap gap-2">
      <div style="position: relative; display: block;float: left;width: 100%;">
        <el-descriptions title="销售信息" :column="4">
          <el-descriptions-item label="姓名：" :span="4">
            张三
          </el-descriptions-item>
          <el-descriptions-item label="当前操作：">
            给张三分配客户
          </el-descriptions-item>
          <el-descriptions-item label="已选客户数：">
            {{ checkedTotal }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <el-form ref="searchCustomerFormRef" :model="searchCustomerForm" :inline="true" label-width="auto" style="width: 100%">
        <el-form-item label="客户" prop="name">
          <el-input v-model="searchCustomerForm.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background-color: #409EFF; border-color: #409EFF;" :icon="Search" @click="searchCustomer">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="CustomerTableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="100" />
        <el-table-column prop="name" label="客户姓名" width="120" />
        <el-table-column prop="phone" label="手机号" width="120" />
        <el-table-column prop="level" label="客户类型" width="120">
          <template #default="scope">
            <span v-if="scope.row.level === 1">潜在客户</span>
            <span v-if="scope.row.level === 2">一般客户</span>
            <span v-if="scope.row.level === 3">优质客户</span>
            <span v-if="scope.row.level === 4">VIP客户</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" width="180" />
        <el-table-column prop="id" label="选择" width="100">
          <template #default="{ row }">
            <!-- <ElCheckbox v-model="row.checked">
            </ElCheckbox> -->
            <el-checkbox v-model="row.checked" @change="checkHandle" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <template #footer>
      <el-button type="primary" @click="submitShareFormHandle()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<style scoped>
 .share-form {
  width: 700px;
 }
</style>
